package top.lvkun.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.PageList;
import top.lvkun.org.domain.Department;
import top.lvkun.org.domain.Employee;
import top.lvkun.org.query.DepartmentQuery;
import top.lvkun.org.query.EmployeeQuery;
import top.lvkun.org.service.IDepartmentService;
import top.lvkun.org.service.IEmployeeService;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;

    @GetMapping("{id}")
    @ApiOperation(value = "通过员工id查询",notes = "通过员工id查询")
    public Employee getById(@PathVariable("id") Long id){
        return employeeService.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            System.out.println(employee);
            if (employee.getId() == null) {
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除数据",notes = "通过员工id删除数据")
    public AjaxResult delById(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "查询所有")
    public List<Employee> getAll(){
        return employeeService.getAll();
    }

    //分页+高级查询
    @PostMapping
    @ApiOperation(value = "分页+高级查询",notes = "参数EmployeeQuery")
    public PageList<Employee> getPage(@RequestBody EmployeeQuery query){
        return employeeService.queryData(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "参数ids")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }


}
